/**
 * @description: 全局组件挂载
 */
const installComponent = {
  /**
  * @description: kebab-case转PascalCase
  * @param { String } fileName 文件名称
  */
  fileLineToHumpName(fileName: string) {
    let fileStr = "",
      _fileArr = [];
    if (fileName.includes("-")) _fileArr = fileName.split("-");
    else return fileName;
    const caseArr = _fileArr.map((str) =>
      str.toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase())
    );
    caseArr.forEach((item) => (fileStr += item));
    return fileStr || fileName;
  },
  install(app: any) {
    const requireComponent = require.context(
      "@/components/common",
      false,
      /\.vue$/
    );
    const modulesComponent = requireComponent.keys().map((path) => {
      const component = {};
      const moduleName = path.replace(/^\.\/(.*)\.\w+$/, "$1");
      component[moduleName] = requireComponent(path).default;
      return component;
    });
    modulesComponent.map((component) =>
      Object.keys(component).forEach((name) => {
        app.component(component[name]?.name ? this.fileLineToHumpName(component[name].name) : this.fileLineToHumpName(name), component[name])
      }
      )
    );
  }
}

export default installComponent